package com.wbl.train.member.controller;
import com.nimbusds.jose.JOSEException;
import com.wbl.train.common.member.dto.MemberDto;
import com.wbl.train.common.member.entity.MemberEntity;
import com.wbl.train.common.member.vo.MemberVo;
import com.wbl.train.common.result.R;
import com.wbl.train.member.service.MemberService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 王博纶
 * @since 2023-12-19
 * @description 会员
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/member")
@Tag(name = "会员控制器类",
        description = "MemberController 会员 后端数据接口")
public class MemberController {

    private MemberService memberService;

    @GetMapping("/count")
    public R<Integer> count(){
        return R.ok(Math.toIntExact(memberService.count()));
    }

    /**
     * 注册功能
     * @return id
     */
    @PostMapping("/register")
    public R<Boolean> register(@Valid MemberEntity memberEntity){
        return R.ok("注册成功",memberService.register(memberEntity));
    }


    /**
     * 验证码发送
     */
    @PostMapping("/sendCode")
    public R<MemberVo> sendCode(@RequestBody @Valid MemberEntity memberEntity){
        return R.ok("验证码发送成功",memberService.sendCode(memberEntity));
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public R<MemberVo> login(@RequestBody @Valid MemberDto memberDto) throws JOSEException {
        return R.ok("登陆成功",memberService.login(memberDto));
    }

}
